package com.project.demo.controller;

import com.project.demo.entity.Upload;
import com.project.demo.service.UploadService;

import com.project.demo.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * (Upload)表控制层
 */
@RestController
@RequestMapping("upload")
public class UploadController extends BaseController<Upload, UploadService> {
    /**
     * 服务对象
     */
    @Autowired
    public UploadController(UploadService service) {
        setService(service);
    }

    @GetMapping("/avatar/{fileName}")
    public void avatar(@PathVariable String fileName, HttpServletResponse response) {
        File file = new File("G:\\Java idea space\\OnlineRecruitment\\target\\classes\\static\\upload\\" + fileName);
        byte[] bytes = new byte[1024];
        try (OutputStream os = response.getOutputStream();
             FileInputStream fis = new FileInputStream(file)) {
            while ((fis.read(bytes)) != -1) {
                os.write(bytes);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @GetMapping("/articleCovers/{fileName}")
    public void articleCovers(@PathVariable String fileName, HttpServletResponse response) {
        File file = new File("G:\\Java idea space\\OnlineRecruitment\\target\\classes\\static\\upload\\" + fileName);
        byte[] bytes = new byte[1024];
        try (OutputStream os = response.getOutputStream();
             FileInputStream fis = new FileInputStream(file)) {
            while ((fis.read(bytes)) != -1) {
                os.write(bytes);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @GetMapping("/resume/{fileName}")
    public void resume(@PathVariable String fileName, HttpServletResponse response) {
        try {
            File file = new File("G:\\Java idea space\\OnlineRecruitment\\target\\classes\\static\\upload\\" + fileName);
            String ext = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStream fis = new BufferedInputStream(fileInputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}


